package com.peter.recyclerviewdemo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    @Nullable
    private List<TestData> testDataList;

    public TestAdapter(@Nullable List<TestData> testDataList) {
        this.testDataList = testDataList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_demo, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (testDataList != null) {
            TestData testData = testDataList.get(position);
            ((ViewHolder) holder).tv.setText(testData.textMessage);
            ((ViewHolder) holder).bt.setText(testData.btnText);
            switch (position) {
                case 0:
                    ((ViewHolder) holder).tv.setText("测试同步未开启1");
                    break;
                case 1:
                    ((ViewHolder) holder).tv.setText("测试同步中");
                    break;
                case 2:
                    ((ViewHolder) holder).tv.setText("测试同步未开始");
                    break;
                case 3:
                    ((ViewHolder) holder).tv.setText("测试同步结束");
                    break;
                case 4:
                    ((ViewHolder) holder).tv.setText("测试Customview");
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    public int getItemCount() {
        return testDataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @NonNull
        TextView tv;
        @NonNull
        Button bt;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.text_item_test);
            bt = itemView.findViewById(R.id.btn_item_test);
        }
    }
}
